package EvaTCPL;
import java.util.*;
import java.io.*;
//import java_cup.runtime.*;


public class EvaTCPLProblem
{
  /** Number of control steps. Default = 1. */ 
  public int controlsteps = -1;

/** Number of evaluation steps. Default = 1. */ 
  public int evalsteps = -1;

  /** Verbose. 0=completely quiet (except parse errors), 1=warnings. Default = 0. */ 
  public int verbose = 0;

  public Vector variables = new Vector();

  public Vector functions = new Vector();

  /** Filename of the file to load. */
  public String thefilename;

  /** The current timestep. */
  public int currenttime = 0;

  /** Temporary variables used in the initialization phase. */
  public String init_curvarname;


  static Yylex thescanner;
  static parser theparser;
  //static Symbol RES;

  /** variables for checking. */
  StringBuffer errormsgs;
  int errors = 0;
  int warnings = 0;

/** Create a new testproblem. 
    @param filename The file containing the problem specification.
*/
  public EvaTCPLProblem(String filename)
    {
      super();
      
      Enumeration ennum;

      thefilename = filename;

      FileInputStream FilePtr;
      try
	{
	  FilePtr = new FileInputStream(thefilename);
	  thescanner = new Yylex(new InputStreamReader(FilePtr));

	  try
	    {
	      System.out.print("Loading and parsing.........");
	      //theparser = new parser(thescanner); 
	      /*theparser.theproblem = this;
	      //	    RES = theparser.debug_parse();
	      RES = theparser.parse();*/
	      System.out.println("Done!");

	      errormsgs = new StringBuffer();

	      // Check correctness of all variables and functions
	      for (ennum = variables.elements();ennum.hasMoreElements();) {
		((EvaTCPLVariable)ennum.nextElement()).checkVariable(this);
	      }

	      if (errormsgs.length()>0) {
		System.out.println("******** SYNTAX ERRORS IN INPUT FILE ********\n");
		System.out.println(errormsgs.toString());
		System.out.println("----\nNo. of errors: "+errors);
		System.exit(0);
	      }

	      reset();
	    }
	  catch(Exception e)
	    {
	      System.err.println(e);
	    }
	}
      catch (IOException e)
	{
	  System.err.println(e);
	}



    }

  public EvaTCPLVariable findVariable(String identifier)
    {
      EvaTCPLVariable curvar;
      int i;

      for (i=0; i<variables.size(); i++) {
	curvar = (EvaTCPLVariable)variables.elementAt(i);
	if (curvar.identifier.equals(identifier))
	  return curvar;
      }
      return null;
    }

  public void addVariable(EvaTCPLVariable var)
    {
      System.out.println(var.identifier);
      variables.add(var);
    }

  //  public void updateProblem(EvaTCPLController controller, int timesteps)
  public void updateProblem(int timesteps)
    {
      int i;
      Enumeration ennum;

      for (i=0;i<timesteps; i++) {
	currenttime++;

	if (verbose>0) {
	  System.out.println("##### TIME: "+currenttime);
	}

	for (ennum = variables.elements();ennum.hasMoreElements();) {
	  ((EvaTCPLVariable)ennum.nextElement()).prepareUpdate();
	}

	for (ennum = variables.elements();ennum.hasMoreElements();) {
	  ((EvaTCPLVariable)ennum.nextElement()).finalizeUpdate();
	}

	// Print current values
	if (verbose>0) {
	  for (ennum = variables.elements();ennum.hasMoreElements();) {
	    System.out.println(((EvaTCPLVariable)ennum.nextElement()).toString());
	  }
	}

      }
    }

  public void reset()
    {
      Enumeration ennum;
      EvaTCPLVariable curvar;

      if (verbose>0) {
	System.out.println("##### TIME: 0 (After initialization)");
      }

      for (ennum = variables.elements();ennum.hasMoreElements();) {
	curvar = (EvaTCPLVariable)ennum.nextElement();
	init_curvarname = curvar.identifier;
	curvar.initialize();

	if (verbose>0) {
	    System.out.println(curvar.toString());
	  }
      }
    }

}
